<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>学生课程成绩信息页面</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <script src="/js/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="/plugins/bootstraptable/bootstrap-table.min.css">


</head>
<body>
<div class="container-fluid">




    <button type="button" class="logout" name="logout" onclick="out();">退出登录</button>


    <br>这是学生成绩信息表单：</br>

<!--    <form class="form-inline">-->
<!--        <div class="form-group">-->
<!--            <label for="condition">请输入姓名</label>-->
<!--            <input type="text" class="form-control" id="condition" name="condition" placeholder="请输入名字">-->
<!--        </div>-->
<!--        <button type="button" class="btn btn-default btn-primary" onclick="search();">查询</button>-->
<!--    </form>-->

    <table class="table table-striped" id="stuTable">

    </table>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">
                <form id="formStudent">


                    <!--                    <input hidden="hidden" id="id" name="id"/>  &lt;!&ndash;隐藏读取id&ndash;&gt;-->
                    <div class="form-group">
                        <label for="studentId">学号</label>
                        <input type="text" class="form-control" id="studentId" name="studentId"  placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="studentName">姓名</label>
                        <input type="text" class="form-control" id="studentName" name="studentName"  placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别:</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="男">男</option>
                            <option value="女">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="courseName">课程名</label>
                        <input type="text" class="form-control" id="courseName" name="courseName"  placeholder="课程名">
                    </div>
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score"  placeholder="成绩">
                    </div>
                </form>
                <!--表单结束-->

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveStudent();">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


</div>


<script src="/js/bootstrap.min.js"></script>
<script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>


<script type="text/javascript">

    //退出登录
    function out(){
        var result = confirm("确定要退出吗？");
        if(result){
            location.href="/tologout";
        }
    }




    //查找
    function search() {

        $('#stuTable').bootstrapTable("destroy");   //删除原来新建的表格,then重建表格
        loadTable();
    }

    function preAdd(){
        $("#id").val(0);//提示新增
    }


    //新增或保存
    function saveStudent(){
        $('#stuTable').bootstrapTable("destroy");

        var data=$("#formStudent").serialize();//提交的新学生信息

        var id=$("#id").val();
        if(id<1){
            //新增id
            $.ajax({
                url:"/api/sc/insert",
                method:"post",
                data:data
            }).done(function (){
                loadTable();
                $('#myModal').modal('hide')
            })
        }else {
            //更新id
            $.ajax({
                url: "/api/sc/update",
                method: "put",
                data: data
            }).done(function () {
                name:'';
                loadTable();
                $('#myModal').modal('hide')
            })
        }
    }


    //编辑
    function editStudent(studentId){
        //$('#stuTable').bootstrapTable("destroy");

        $('#myModal').modal('show');
        $.ajax({
            url:'/api/sc/get/'+studentId
        }).done(function (rs){

            $("#studentId").val(rs.studentId);
            $("#studentName").val(rs.studentName);
            $("#sex").val(rs.sex);
            $("#courseName").val(rs.courseName);
            $("#score").val(rs.score)

        })

    }

    //删除
    function  deleteStudent(studentId){
        $('#stuTable').bootstrapTable("destroy");

        if(confirm("是否删除")){
            $.ajax({
                url:'/api/sc/delete/'+studentId,
                method:"delete"

            }).done(function (){
                name = '';
                loadTable();

            });
        }

    }


    function loadTable(){
        $('#stuTable').bootstrapTable({
            url: '/api/sc/getbypageSc',
            striped: true, //隔行变色
            pagination: true, //是否分页
            singleSelect: true, //禁用多选
            pageSize: 3,    //每页数据条数
            pageNumber: 1,  //首页页码
            sidePagination: "server",   //服务器端分页
            queryParams:function (params){

                var paraObj = {
                    size:params.limit,
                    page:params.offset/params.limit,
                    sort:"id",
                    direct:"desc",
                    condition: $("#condition").val()    //取到searchName框中的值
                };

                return paraObj;
            },
            columns: [{
                field: 'studentId',
                title: '学号'
            }, {
                field: 'studentName',
                title: '姓名'
            }, {
                field: 'sex',
                title: '性别'
            }, {
                field: 'courseName',
                title: '课程名'
            }, {
                field: 'score',
                title: '成绩'
            } ]
             //    {
             //     filed:'operate',
             //     title: '删除列',
             //     formatter:function (valu,row,index){
             //         var r='<a class="romove" href="#" title="Remove" onclick="deleteStudent(\''+row.id+'\')">删除</a>';
             //         return r;
             //     }
             // },{
             //     filed:'operate',
             //     title: '编辑列',
             //     formatter:function (valu,row,index){
             //         var r='<a class="Edit" href="#" title="Edit" onclick="editStudent(\''+row.id+'\')">编辑</a>';
             //         return r;
             //     }
             // }]
        });


    }//end of loadTable()


    $(function (){
        loadTable();
    })

</script>
</body>
</html>